package com.hunttown.mes.common;

import com.hunttown.mes.common.utils.FastJsonUtils;
import com.hunttown.mes.common.utils.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * created by wangjunfu on 2019-09-05
 */
public class ApiRequestResults {

    /**
     * 标准返回结果
     *
     * @param httpStatus http请求状态
     * @param result     结果
     * @return JSON
     */
    public static String standardResults(int httpStatus, Object result) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", httpStatus);
        map.put("data", result);

        return FastJsonUtils.toJSONString(map);
    }

    /**
     * 自定义返回结果
     *
     * @param httpStatus  http请求状态
     * @param result      结果
     * @param customKey   自定义参数Key
     * @param customValue 自定义参数值Value
     * @return JSON
     */
    public static String standardResults(int httpStatus, Object result, String customKey, Object customValue) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", httpStatus);
        map.put("data", result);

        if (StringUtils.isNotBlank(customKey) && !Objects.equals(customValue, null)) {
            map.put(customKey, customValue);
        }

        return FastJsonUtils.toJSONString(map);
    }

    /**
     * 自定义返回结果
     *
     * @param httpStatus  http请求状态
     * @param customKey   自定义参数Key
     * @param customValue 自定义消息
     * @return JSON
     */
    public static String standardResults(int httpStatus, String customKey, String customValue) {
        if (StringUtils.isBlank(customKey)) {
            customKey = "message";
        }

        Map<String, Object> map = new HashMap<>();
        map.put("code", httpStatus);
        map.put("data", 0);
        map.put(customKey, customValue);

        return FastJsonUtils.toJSONString(map);
    }
}
